package club.kingyin.commons.utils;


import club.kingyin.commons.response.Result;
import com.alibaba.fastjson.JSON;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class RequestUtils {

    public static <T> T getData(String json, Class<T> type) {
        return type.cast(JSON.parseObject(json, Result.class).getData());
    }

    public static Object getData(String json) {
        return getResult(json).getData();
    }

    public static Result getResult(String json) {
        return JSON.parseObject(json, Result.class);
    }

    public static String getMessage(String json) {
        return getResult(json).getMessage();
    }

    public static String getCode(String json) {
        return getResult(json).getCode();
    }

    public static String headerDecoder(String data) {
        return new String(Base64.getDecoder().decode(data), StandardCharsets.UTF_8);
    }

    public static String headerEncoder(String data) {
        return Base64.getEncoder().encodeToString(data.getBytes(StandardCharsets.UTF_8));
    }
}
